<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var swapPairs = function (head) {
      if (head === null || head.next === null) {
        return head;
      }
      const newHead = head.next;
      head.next = swapPairs(newHead.next);
      newHead.next = head;
      return newHead;
    };
    console.log(swapPairs([1, 2, 3, 4]));
  </script>
</body>

</html>